package com.efeichong.project.service.impl;

import com.efeichong.common.PageData;
import com.efeichong.common.exception.CustomException;
import com.efeichong.common.utils.OptionEmptyFunction;
import com.efeichong.jpa.JExample;
import com.efeichong.project.domain.dao.db.SysDictDetailDao;
import com.efeichong.project.domain.entity.db.SysDictDetail;
import com.efeichong.project.domain.vo.SysDictDetailVo;
import com.efeichong.project.service.SysDictDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author
 * @date 2020/11/25
 * @description 数据字典详细
 */
@RequiredArgsConstructor
@Service
public class SysDictDetailServiceImpl implements SysDictDetailService {

    private final SysDictDetailDao sysDictDetailDao;

    /**
     * 通过id查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public SysDictDetailVo getInfo(Long id) {
        return sysDictDetailDao.selectById(id, SysDictDetailVo.class).orElseThrow(OptionEmptyFunction.create());
    }

    /**
     * 分页查询
     *
     * @param sysDictDetailVo
     * @return
     */
    @Override
    public PageData<SysDictDetailVo> selectByPage(SysDictDetailVo sysDictDetailVo) {
        JExample<SysDictDetail> example = new JExample();
        example.orderBy().asc("sort");
        example.initExample(sysDictDetailVo);
        example.and().andEqualTo("dict.id", sysDictDetailVo.getDictId());
        return sysDictDetailDao.selectByPage(example, SysDictDetailVo.class);
    }

    /**
     * 新增
     *
     * @param sysDictDetailVo
     * @return
     */
    @Transactional
    @Override
    public void insertSysDictDetail(SysDictDetailVo sysDictDetailVo) {
        sysDictDetailDao.save(sysDictDetailVo.toPo(SysDictDetail.class));
    }

    /**
     * 修改
     *
     * @param sysDictDetailVo
     * @return
     */
    @Transactional
    @Override
    public void updateSysDictDetail(SysDictDetailVo sysDictDetailVo) {
        if (sysDictDetailVo.getId() == null) {
            throw new CustomException("主键为必传参数");
        }
        sysDictDetailDao.save(sysDictDetailVo.toPo(SysDictDetail.class));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        sysDictDetailDao.deleteAllByIds(ids);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void deleteById(Long id) {
        sysDictDetailDao.deleteById(id);
    }
}
